import 'package:flutter/material.dart';

void main() => runApp(LoginPage());

class LoginPage extends StatelessWidget {
  GlobalKey<FormState> _loginKey = GlobalKey();
  String _username;
  String _password;

  void login() {
    var currentState = _loginKey.currentState;
    if(currentState.validate()) {
      currentState.save();
     print("name == $_username password == ${_password}");
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text("form"),
        ),
        body: Column(
          children: <Widget>[
            Container(
              padding: const EdgeInsets.all(10),
              child: Form(
                key: _loginKey,
                child: Column(
                  children: <Widget>[
                    TextFormField(
                      decoration: InputDecoration(labelText: '请输入用户名'),
                      onSaved: (value) {
                        print("onSave 用户名：$value");
                        _username = value;
                      },
                      onFieldSubmitted: (value) {
                        print('onFieldSubmitted用户名:$value');
                      },
                    ),
                    TextFormField(
                      decoration: InputDecoration(labelText: '请输入密码'),
                      onSaved: (value) {
                        print("onSave 密码：$value");
                        _password = value;
                      },
                      obscureText: true, // 密码*显示
                      onFieldSubmitted: (value) {
                        print('onFieldSubmitted密码:$value');
                      },
                      validator: (value) {
                        return value.length < 10 ? '密码长度不够10' : null;
                      },
                    ),
                    Container(
                      margin: EdgeInsets.only(top: 10),
                      width: 400,
                      height: 40,
                      child: RaisedButton(
                        onPressed: () {
                          login();
                        },
                        child: Text("登陆"),
                      ),
                    )
                  ],
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}
